package sqy.bookstore.admin.adminDao;

import java.sql.SQLException;
import java.util.*;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.itcast.jdbc.TxQueryRunner;
import sqy.bookstore.book.domain.Book;


public class AdminselectDaoImpl{
	private QueryRunner qr = new TxQueryRunner();
	String sql=null; 
	public List<Book> findSelect(String cname,String bname)  {
		//定义参数的集合
        List<Object> params = new ArrayList<Object>();
		
		try{
			if((cname!=null && cname.length()>0) && (bname==null || bname.length()==0)){
	        	sql = "select cid from category where cname  like   ? ";
	        	//params.add("%"+cname+"%");
	            String cid =(String) qr.query(sql, new ScalarHandler(), ("%"+cname+"%").toString());
	            System.out.println(2);
	        	sql = "select * from book where del=false and cid=?";
	            return qr.query(sql, new BeanListHandler<Book>(Book.class),cid);
	        }
	        if((cname!=null && cname.length()>0) && (bname!=null && bname.length()>0)){
	        	sql = "select cid from category where  cname  like  ? ";
	        	//params.add("%"+cname+"%");
	            String cid =(String) qr.query(sql, new ScalarHandler(), ("%"+cname+"%").toString());
	            System.out.println(cid);
	            System.out.println(3);
	            
	            sql = "select * from book where del=false and cid=? and  bname  like  ? ";
	            return qr.query(sql, new BeanListHandler<Book>(Book.class),cid,("%"+bname+"%").toString());
	        }
	        if((cname==null || cname.length()==0) && (bname!=null && bname.length()>0)){
	        	sql = "select * from book where del=false and  bname  like  ?";
	        	System.out.println(4);
	            return qr.query(sql, new BeanListHandler<Book>(Book.class),("%"+bname+"%").toString());
	        }
	        /*if((cname==null || cname.length()==0) && (bname==null || bname.length()==0)){
	        	return "r:/adminjsps/admin/index.jsp";
	        }*/
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
   
		System.out.println(5);
		return null;
        
        
       
 
		 
		

        
  }

}
